package com.party.core.service.member.impl;

import com.google.common.base.Strings;
import com.party.common.paging.DataParam;
import com.party.common.paging.Page;
import com.party.common.utils.DateUtils;
import com.party.core.dao.read.member.MemberMerchantCountReadDao;
import com.party.core.dao.write.member.MemberMerchantCountWriteDao;
import com.party.core.model.BaseModel;
import com.party.core.model.YesNoStatus;
import com.party.core.model.member.MemberMerchantCount;
import com.party.core.model.member.MemberMerchantCountQuery;
import com.party.core.model.member.MemberMerchantCountWithMember;
import com.party.core.model.member.MemberMerchantDetailQuery;
import com.party.core.service.member.IMemberMerchantCountService;
import com.sun.istack.NotNull;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * 合作商数据总统计服务实现
 * Created by wei.li.
 *
 * @date 2017/12/12 0012
 * @time 9:31
 */
@Service
public class MemberMerchantCountService implements IMemberMerchantCountService {

    @Autowired
    private MemberMerchantCountReadDao memberMerchantCountReadDao;

    @Autowired
    private MemberMerchantCountWriteDao memberMerchantCountWriteDao;

    /**
     * 新增
     * @param memberMerchantCount 统计数据
     * @return 编号
     */
    @Override
    public String insert(MemberMerchantCount memberMerchantCount) {
        BaseModel.preInsert(memberMerchantCount);
        boolean result = memberMerchantCountWriteDao.insert(memberMerchantCount);
        if (result){
            return memberMerchantCount.getId();
        }
        return null;
    }

    /**
     * 更新
     * @param memberMerchantCount 统计数据
     * @return 更新结果(true/false)
     */
    @Override
    public boolean update(MemberMerchantCount memberMerchantCount) {
        memberMerchantCount.setUpdateDate(DateUtils.today());
        return memberMerchantCountWriteDao.update(memberMerchantCount);
    }

    /**
     * 逻辑删除
     * @param id 实体主键
     * @return 删除结果（true/false）
     */
    @Override
    public boolean deleteLogic(@NotNull String id) {
        if (Strings.isNullOrEmpty(id)){
            return false;
        }
        return memberMerchantCountWriteDao.deleteLogic(id);
    }

    /**
     * 物理删除
     * @param id 实体主键
     * @return 删除结果（true/false）
     */
    @Override
    public boolean delete(@NotNull String id) {
        if (Strings.isNullOrEmpty(id)){
            return false;
        }
        return memberMerchantCountWriteDao.delete(id);
    }

    /**
     * 批量逻辑删除
     * @param ids 主键集合
     * @return 删除结果（true/false）
     */
    @Override
    public boolean batchDeleteLogic(@NotNull Set<String> ids) {
        if (CollectionUtils.isEmpty(ids)){
            return false;
        }
        return memberMerchantCountWriteDao.batchDeleteLogic(ids);
    }

    /**
     * 批量物理删除
     * @param ids 主键集合
     * @return 删除结果（true/false）
     */
    @Override
    public boolean batchDelete(@NotNull Set<String> ids) {
        if (CollectionUtils.isEmpty(ids)){
            return false;
        }
        return memberMerchantCountWriteDao.batchDelete(ids);
    }

    /**
     * 获取统计信息
     * @param id 主键
     * @return 统计信息
     */
    @Override
    public MemberMerchantCount get(String id) {
        return memberMerchantCountReadDao.get(id);
    }


    /**
     * 获取或者设置
     * @param memberId 会员编号
     * @return 机构数据统计
     */
    @Override
    public MemberMerchantCount getSet(String memberId) {
        MemberMerchantCount memberMerchantCount = memberMerchantCountReadDao.findByMemberId(memberId);
        if (null != memberMerchantCount){
            return memberMerchantCount;
        }
        else {
            memberMerchantCount = new MemberMerchantCount(0, 0, 0, 0d, 0L, 0, 0);
            memberMerchantCount.setMemberId(memberId);
            this.insert(memberMerchantCount);
        }
        return memberMerchantCount;
    }

    /**
     * 根据会员编号查询
     * @param memberId 会员编号
     * @return 统计数据
     */
    @Override
    public MemberMerchantCount findByMemberId(String memberId) {
        MemberMerchantCount memberMerchantCount = memberMerchantCountReadDao.findByMemberId(memberId);
        if (null == memberMerchantCount){
            return new MemberMerchantCount(0, 0, 0, 0d, 0L, 0, 0);
        }
        return memberMerchantCount;
    }

    /**
     * 分页查询
     * @param memberMerchantCount 统计信息
     * @param page 分页信息
     * @return 统计信息列表
     */
    @Override
    public List<MemberMerchantCount> listPage(MemberMerchantCount memberMerchantCount, Page page) {
        return memberMerchantCountReadDao.listPage(memberMerchantCount, page);
    }

    /**
     * 统计信息列表
     * @param memberMerchantCount 统计信息
     * @return 统计信息列表
     */
    @Override
    public List<MemberMerchantCount> list(MemberMerchantCount memberMerchantCount) {
        return memberMerchantCountReadDao.listPage(memberMerchantCount, null);
    }

    /**
     * 统计查询
     * @param memberMerchantCountQuery 查询参数
     * @param page 分页参数
     * @return 统计列表
     */
    @Override
    public List<MemberMerchantCountWithMember> queryList(MemberMerchantCountQuery memberMerchantCountQuery, DataParam dataParam, Page page) {
        if (YesNoStatus.YES.getCode().equals(memberMerchantCountQuery.getIsQueryChild())) {
            return memberMerchantCountReadDao.queryListWithChild(memberMerchantCountQuery, dataParam, page);
        } else {
            return memberMerchantCountReadDao.queryList(memberMerchantCountQuery, dataParam, page);
        }
    }

    @Override
    public List<MemberMerchantCount> batchList(@NotNull Set<String> ids, MemberMerchantCount memberMerchantCount, Page page) {
        return null;
    }

    /**
     * 统计所有统计数据
     * @return 统计所有统计数据
     */
    @Override
    public MemberMerchantCount sum() {
        MemberMerchantCount memberMerchantCount = memberMerchantCountReadDao.sum();
        if (null != memberMerchantCount){
            return memberMerchantCount;
        }
        return new MemberMerchantCount(0, 0, 0, 0d, 0L, 0, 0);
    }

    @Override
    public MemberMerchantCount findByMemberIdSum(String memberId, boolean isQueryChild) {
        MemberMerchantDetailQuery detailQuery = new MemberMerchantDetailQuery();
        detailQuery.setMemberId(memberId);
        detailQuery.setIsQueryChild(isQueryChild ? 1 : 0);
        List<MemberMerchantCountWithMember> list
                = this.queryList(MemberMerchantCountQuery.transform(detailQuery), null, null);
        if (!CollectionUtils.isEmpty(list)) {
            return list.get(0);
        }
        return new MemberMerchantCount(0, 0, 0, 0d, 0L, 0, 0);
    }
}
